package de.bml.versandanzeige;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Standort {

	public static final String TABLE_NAME = "standort";
	public static final String COLUMN_LABEL = "label";
	public static final String COLUMN_DETAILS = "details";
	public static final String COLUMN_ID_EXTERN = "id_extern";

	// Datebase fields
	private static SQLiteOpenHelper dbHelper;
	private static SQLiteDatabase db;

	// Database creation sql statement
	private static final String TABLE_CREATE = "create table " + TABLE_NAME
			+ "( " + SQLiteOpenHelper.COLUMN_ID
			+ " integer primary key autoincrement, " + COLUMN_LABEL
			+ " text not null, " + COLUMN_DETAILS + " text," + COLUMN_ID_EXTERN
			+ " text)";

	private static Standort aktuell;

	private String id;

	public Standort(String label, String details) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_LABEL, label);
		values.put(COLUMN_DETAILS, details);
		values.put(COLUMN_ID_EXTERN, "");
		id = Long.toString(db.insert(TABLE_NAME, null, values));
	}

	public Standort(String label) {
		this(label, "");
	}

	public Standort(long id) {
		this.id = Long.toString(id);
	}

	public long getId() {
		return Long.valueOf(id);
	}

	public static Standort get(long id) {
		String label = getStringColumn(COLUMN_LABEL, String.valueOf(id));
		if (label.equals("")) {
			return null;
		} else {
			return new Standort(id);
		}
	}

	public static Standort create(String label) {
		return new Standort(label);
	}

	public static Standort create(String label, String details) {
		return new Standort(label, details);
	}

	public String getLabel() {
		return getStringColumn(COLUMN_LABEL);
	}

	public Standort setLabel(String label) {
		return setStringColumn(COLUMN_LABEL, label);
	}

	public String getDetails() {
		return getStringColumn(COLUMN_DETAILS);
	}

	public Standort setDetails(String details) {
		return setStringColumn(COLUMN_DETAILS, details);
	}

	public String getIdExtern() {
		return getStringColumn(COLUMN_ID_EXTERN);
	}

	public Standort setIdExtern(String value) {
		return setStringColumn(COLUMN_ID_EXTERN, value);
	}

	private String getStringColumn(String column) {
		return getStringColumn(column, id);
	}

	private static String getStringColumn(String column, String id) {
		Cursor c = db.query(TABLE_NAME, new String[] { column },
				SQLiteOpenHelper.COLUMN_ID + "=?", new String[] { id }, null,
				null, null);
		c.moveToFirst();
		String result = "";
		if (!c.isAfterLast()) {
			result = c.getString(0);
		}
		c.close();
		return result;
	}

	private Standort setStringColumn(String column, String value) {
		ContentValues values = new ContentValues();
		values.put(column, value == null ? "" : value);
		db.update(TABLE_NAME, values, SQLiteOpenHelper.COLUMN_ID + "=?",
				new String[] { id });
		return this;
	}

	// Will be used be the ArrayAdapter of a ListView
	@Override
	public String toString() {
		return getLabel();
	}

	public static void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
	}

	public static void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public void delete() {
		System.out.println("Standort deleted with id: " + id);
		db.delete(TABLE_NAME, SQLiteOpenHelper.COLUMN_ID + " = " + id, null);
		id = null;
	}

	public static void deleteAll() {
		System.out.println("All Standort deleted.");
		db.delete(TABLE_NAME, null, null);
	}

	public static List<Standort> getAll() {
		List<Standort> standorte = new ArrayList<Standort>();

		Cursor cursor = db.query(TABLE_NAME,
				new String[] { SQLiteOpenHelper.COLUMN_ID }, null, null, null,
				null, COLUMN_LABEL);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			standorte.add(new Standort(cursor.getLong(0)));
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return standorte;
	}

	public static void reconnectDB() {
		db = dbHelper.getWritableDatabase();
	}

	public static void disconnectDB() {
		db.close();
	}

	public static void init(Context context) {
		dbHelper = new SQLiteOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public static Standort getAktuell() {
		return aktuell;
	}

	public static void setAktuell(Standort aktuell) {
		Standort.aktuell = aktuell;
	}

}
